package com.android.tea.adapter;

import java.util.List;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tea.R;
import com.android.tea.entity.Order;
import com.android.tea.util.StringUtils;

public class OrdersAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<Order> list;
	private Context context;
	
	public OrdersAdapter(Context context,List<Order> list) {
		super();
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Order getItem(int position) {
		if (getCount() >= position + 1) {
			return list.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Order order = list.get(position);
		if (order == null) {
			return null;
		}
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.order_item, null);
			holder = new ViewHolder();
			holder.item = (RelativeLayout) convertView.findViewById(R.id.relativeLayout1);
			holder.tvOrderNumber = (TextView) convertView.findViewById(R.id.ordernumber);
			holder.tvPrice = (TextView) convertView.findViewById(R.id.price);
			holder.tvOrderCreate = (TextView) convertView.findViewById(R.id.order_ceate);
			holder.tvOrderStatus = (TextView) convertView.findViewById(R.id.order_status);
			holder.id = position;
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.relativeLayout1:
					
					break;
				default:
					break;
				}
			}

			
		};
		
		holder.item.setOnClickListener(clickListener);
		
		setData(order,holder);
		return convertView;
	}
	
	static class ViewHolder {
		int id;
		RelativeLayout item;
		TextView tvOrderNumber;
		TextView tvPrice;
		TextView tvOrderCreate;
		TextView tvOrderStatus;
	}

	private void setData(Order info, ViewHolder holder) {
		String ordernumber = info.getOrderNumber();
		String price = info.getPrice();
		long createtime = info.getCreatetime();
		int status = info.getStatus();
		
		if (StringUtils.isNotNullOrEmpty(ordernumber)) {
			holder.tvOrderNumber.setText(formatString(context.getResources().getString(R.string.ordernumber), ordernumber));
		}else {
			holder.tvOrderNumber.setText(context.getResources().getString(R.string.unknown));
		}
		
		String formatString = formatString(context.getResources().getString(R.string.price), price);
		holder.tvPrice.setText(changeSytle(formatString,price));
		
		if (createtime > 0) {
			holder.tvOrderCreate.setText(formatString(context.getResources().getString(R.string.ordertime),StringUtils.getFormatTime(createtime)));
		}else {
			holder.tvOrderCreate.setText(context.getResources().getString(R.string.ordertime) + context.getResources().getString(R.string.unknown));
		}
		
		setStatus(holder.tvOrderStatus,status);
		
	}
	
	private SpannableString changeSytle(String str,String price) {
		SpannableString ss = new SpannableString(str);
		ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red)), (str.length()-price.length()), str.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//		ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")), 0, str.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return ss;
	}
	
	private String formatString(String str,String format) {
		return String.format(str, format);
	}

	private void setStatus(TextView tvOrderStatus, int status) {
		switch (status) {
		case 0://等待发货
			tvOrderStatus.setTextColor(context.getResources().getColor(R.color.red));
			tvOrderStatus.setText(context.getResources().getString(R.string.status_wait));
			break;
		case 1://正在发货
			tvOrderStatus.setTextColor(context.getResources().getColor(R.color.black));
			tvOrderStatus.setText(context.getResources().getString(R.string.status_deliver));
			break;
		case 2://已经收货
			tvOrderStatus.setTextColor(context.getResources().getColor(R.color.black));
			tvOrderStatus.setText(context.getResources().getString(R.string.status_receive));
			break;

		default:
			break;
		}
	}

}
